package com.daolion.base.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.view.WindowManager;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author miaoxiongfei@foxmail.com
 * @date 2016-06-23 15:58
 */
public class ActivityUtil {

    /**
     * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
     * performed by the {@code fragmentManager}.
     */
    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.replace(frameId, fragment);
        }
//        transaction.addToBackStack(null);
        transaction.commit();
    }

    /**
     * @param context
     * @param intent
     */
    public static void switchTo(Context context, Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(intent);
    }



    /**
     * 退出应用
     *
     * @param activity
     */
    public static void exitApp(Activity activity) {
        if (null != activity) {
            activity.finish();
        }
    }


    public static void switchTo(Activity activity, Class<? extends Activity> target, int requestCode) {
        Intent intent = new Intent(activity, target);
        activity.startActivityForResult(intent, requestCode);
        activity.finish();
    }


    public static void setValueToIntent(Intent intent, String key, Object val) {
        if (null == key || null == val) return;
        if (val instanceof Boolean)
            intent.putExtra(key, (Boolean) val);
        else if (val instanceof Boolean[])
            intent.putExtra(key, (Boolean[]) val);
        else if (val instanceof String)
            intent.putExtra(key, (String) val);
        else if (val instanceof String[])
            intent.putExtra(key, (String[]) val);
        else if (val instanceof Integer)
            intent.putExtra(key, (Integer) val);
        else if (val instanceof Integer[])
            intent.putExtra(key, (Integer[]) val);
        else if (val instanceof Long)
            intent.putExtra(key, (Long) val);
        else if (val instanceof Long[])
            intent.putExtra(key, (Long[]) val);
        else if (val instanceof Double)
            intent.putExtra(key, (Double) val);
        else if (val instanceof Double[])
            intent.putExtra(key, (Double[]) val);
        else if (val instanceof Float)
            intent.putExtra(key, (Float) val);
        else if (val instanceof Float[])
            intent.putExtra(key, (Float[]) val);
        else {
            throw new IllegalArgumentException("Not support data Type!");
        }
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            com.yunlin.base.utils.LogUtil.w(appProcess.processName + "　" + appProcess.importance);
            if (appProcess.processName.equals(context.getPackageName())) {
                com.yunlin.base.utils.LogUtil.i("appProcess.importance:" + appProcess.importance);
                /**
                 * public static final int IMPORTANCE_BACKGROUND = 400//后台
                 public static final int IMPORTANCE_EMPTY = 500//空进程
                 public static final int IMPORTANCE_FOREGROUND = 100//在屏幕最前端、可获取到焦点 可理解为Activity生命周期的OnResume();
                 public static final int IMPORTANCE_SERVICE = 300//在服务中
                 public static final int IMPORTANCE_VISIBLE = 200//在屏幕前端、获取不到焦点可理解为Activity生命周期的OnStart();
                 */
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE || appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    com.yunlin.base.utils.LogUtil.i("前台 " + appProcess.processName);
                    return false;
                } else {
                    com.yunlin.base.utils.LogUtil.i("后台 " + appProcess.processName);
                    return true;
                }
            }
        }
        return false;
    }



    private static void setTranslucentStatus(Activity activity, boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    public static Intent buildGalleryIntent(Uri outputUri) {
        Intent intent = null;

        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
        } else {
            intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        intent.setType("image/*");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        return intent;
    }

    public static void switchTo(Activity activity, String classname) {
        String activityFullClassName = getFullClassNameBySimpleClassName(activity, classname);
        if (StringUtils.isNotEmpty(activityFullClassName)) {
            try {
                Class<?> cls = Class.forName(activityFullClassName);
                Intent goIntent = new Intent(activity, cls);
                goIntent.setFlags(goIntent.getFlags());
                switchTo(activity, goIntent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 获取含包名的类名
     *
     * @param context
     * @param simpleName 不含包名的类名
     * @return
     */
    public static String getFullClassNameBySimpleClassName(Context context, String simpleName) {
        String fullClassName = null;
        try {
            String packageName = context.getPackageName();
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            ActivityInfo[] aInfos = pInfo.activities;
            if (aInfos != null) {
                for (ActivityInfo activityInfo : aInfos) {
                    String name = activityInfo.name;
                    if (getClassSimpleName(name).equals(simpleName)) {
                        fullClassName = name;
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return fullClassName;
    }

    /**
     * 获取不含包名的类名
     *
     * @param name 类全名(含包名)
     * @return
     */
    private static String getClassSimpleName(String name) {
        String simpleName = name.substring(name.lastIndexOf(".") + 1);
        return simpleName;
    }
}
